class Solution {
    public String reverseWords(String s) {
        s=s.trim();
        List<String> list=Arrays.asList(s.split("\\s+"));
        Collections.reverse(list);
        return String.join(" ",list);
    }
}

/*
class Solution {
    public String reverseWords(String s) {
        int l=0;
        int r=s.length()-1;
        while(l<=r&&s.charAt(l)==' ')
        {
            l++;
        }
        while(l<=r&s.charAt(r)==' ')
        {
            r--;
        }
        Deque<String> deque=new ArrayDeque<>();
        StringBuilder bu=new StringBuilder();
        while(l<=r)
        {
            char c=s.charAt(l);
            if(c!=' ')
            {
                bu.append(c);
            }
            else if(c==' '&&bu.length()!=0)
            {
                deque.offerFirst(bu.toString());
                bu.setLength(0);
            }
            l++;
        }
        deque.offerFirst(bu.toString());
        return String.join(" ",deque);
    }
}
*/
